package com.vasthu.core.service;

import com.vasthu.core.dao.UserDAOImpl;
import com.vasthu.api.dao.UserDAO;
import com.vasthu.api.domain.User;
import com.vasthu.api.service.UserManagerService;
import com.vasthu.gui.VstTableItemSelectable;
import com.vasthu.utility.VstDBManager;
import java.util.*;
import java.sql.*;

public class UserManagerServiceImpl implements UserManagerService {

	private UserDAO userDao;

	public UserManagerServiceImpl() {
		userDao = new UserDAOImpl();
	}
	
	public boolean insertUser(User user) {
		if (user == null)
			return false;
			
		Connection conn = VstDBManager.get().getConnection();
		boolean ok = false;
		try {
			ok = userDao.insertUser(conn, user);
		} catch (Exception ex) {
			System.out.println("ERROR : UserManagerServiceImpl->insertUser()");
		}
		
		VstDBManager.get().close();
		return ok;
	}
	
	public boolean deleteUserById(long userId) {
		Connection conn = VstDBManager.get().getConnection();
		boolean ok = false;
		try {
			ok = userDao.deleteUserById(conn, userId);
		} catch (Exception ex) {
			System.out.println("ERROR : UserManagerServiceImpl->deleteUserById()");
		}
		
		VstDBManager.get().close();
		return ok;
	}

	public List<VstTableItemSelectable> getAllUsersForTable() {
		List<User> userList = new ArrayList<User>();
		List<VstTableItemSelectable> dataList = new ArrayList<VstTableItemSelectable>();
		
		Connection conn = VstDBManager.get().getConnection();
		
		try {
			userList = userDao.getAllUsers(conn);
			for (User user : userList) {
				dataList.add(user);
			}
		} catch (Exception ex) {
			System.out.println("ERROR : UserManagerServiceImpl->getAllUsersForTable()");
		}
		
		VstDBManager.get().close();
		
		return dataList;
	}

}